package com.decent.text;

import com.decent.util.Md5;
import com.decent.util.RsaUtils;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.RandomStringUtils;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

import static com.decent.text.Text.getCurrentTime;

/**
 * @author 王雪杰
 * @date 2021/8/23 17:16
 */
public class DumasGetParam {
    /**
     * 生成代理商对接授权签名（sign）
     */
    public static void getSign() {
        // TODO: 2021/9/16 hotfix上线后改为驼峰加密
        String str = "mobile=17864720003&proxyCode=18DXQPd&"
                + "proxyUserId=" + "9aWexixkl" +
                // RandomStringUtils.randomAlphanumeric(9) +
                "&timestamp=" + getCurrentTime() + "&key=QINGFpAc";
        //String s = "mobile=17864720012&proxy_code=18DXQPd&"
        //        + "proxy_user_id=" + "3C5BBm3dG" +
        //        "&timestamp=" + getCurrentTime() + "&key=QINGFpAc";

//        String s = "mobile=15277777777&proxyCode=1azxcva&"
//                + "proxyUserId=12as3tx58&timestamp="
//                + getCurrentTime() + "&key=VRbXTlt8s0ult2iER2";
        String sign = Md5.getMd5(str, true, "UTF-8");
        System.out.println(sign);
        // String link = "api.huoqiangshou.cn/seller/proxy/auth/index?"; // 线上地址
        String link = "http://dev.api.chadan.cn/seller/proxy/auth/index?";
        // String link = "172.16.0.247:8921/proxy/auth/index?";
        System.out.println(link + str + "&sign=" + sign);
    }


    public static void mmm() {
        byte[] bytes = Base64.decodeBase64("MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCup0qiSHWt4WZ5e3AcdQ5hw3CjNG+FN67gxpju50EOX4wG1r4pOcSaiESPFcCVUqdZcO3uWWrvPCpTdhI9PKfzqQik08XcoLEZIoSt9zer6Sma//wtqjn+CyaZrkFZ0ga6x8cuWqjJZdFLvcL6DtFc3g4ebwVurhDeHTzgRfZT+wIDAQAB");
        String md5 = Md5.getMd5("12343", true, StandardCharsets.UTF_8.name());
        try {
            byte[] bytes1 = RsaUtils.encryptByPublicKey(md5, Arrays.toString(bytes));
            System.out.println(Arrays.toString(bytes1));
        } catch (Exception e) {
            e.printStackTrace();
        }
//        48, -127, -97, 48, 13, 6, 9, 42, -122, 72, -122, -9, 13, 1, 1, 1, 5, 0, 3, -127, -115, 0, 48, -127, -119, 2, -127, -127, 0, -90, -49, -78, 34, -7, 70, 87, 102, 5, -29, -117, -36, -84, 58, 122, 54, -74, 112, 56, 101, 53, 121, -70, 108, 56, -53, 108, 65, -18, -38, -49, 121, 59, -119, 51, 0, -19, 27, 15, 57, 22, -33, -90, -120, -2, 87, -126, 12, -53, 96, 123, 102, 28, 14, 80, -16, 95, 24, -75, -15, -98, -29, 65, 61, 82, -113, 53, 82, 9, -36, -48, -22, 109, -35, -35, 53, 17, -96, 37, -106, -99, 36, -121, 28, -98, 26, 45, -34, 101, 91, -126, 85, -73, -123, 41, -47, 107, 19, -41, -17, 18, 47, 10, -104, 31, 11, -90, 90, -84, -90, 63, -58, -48, -99, 76, 14, 84, 2, 60, 51, -84, -105, 50, -55, 19, 111, -61, -43, 2, 3, 1, 0, 1
    }

    /**
     * 取得公钥,生成加密密码 (代理商后台登录用)
     *
     * @return 公钥字符串
     */
    public static String getPublicKey() {
        String password = Md5.getMd5("12344", true, StandardCharsets.UTF_8.name());
        System.out.println(password);
        String newKey =
                "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC/E79i4CCKTtuK0ene0f9rS4xtgS6Deu1QHh+htkCVH3jVlwtqHt/qgcIv9pdqU3SNjXPXq/Q7eCC/pjEwfFlh5SDHMh045Sb54Sw4qv35kPhBuwWS+Ct+OXX6AQvYlyl464KyoYIF7mnOHBQ/6vG4VlvyCbTMvYIKXtDsV2bHQwIDAQAB";
        try {
            byte[] encodedData = RsaUtils.encryptByPublicKey(password + "96bc3d03", newKey);
            String encodedDataStr = RsaUtils.encryptBase64(encodedData);
            System.out.println("encodedData:\n" + encodedDataStr);
            System.out.println("encodedDataURL:\n" + URLEncoder.encode(encodedDataStr, "UTF-8"));
            return new String(encodedData);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }


    public static void main(String[] args) throws Exception {
        getSign();
    }

}
